﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.ZSJL.com.Net;
using System.Net;
using System.IO;
using System.Web;
using Service.ZSJL.Com.Utils;

namespace Service.ZSJL.Com.Net
{
    public class AMSMSservice : ISmsService
    {
        private SmsConfig _smsConfig;

        public SmsConfig SmsConfig
        {
            get { return _smsConfig; }
            set { _smsConfig = value; }
        }

        public AMSMSservice()
        {


            //初始化默认配置
            Init();
        }
        /// <summary>
        /// 带参数
        /// </summary>
        /// <param name="smsConfig"></param>
        public AMSMSservice(SmsConfig smsConfig)
        {
            this._smsConfig = smsConfig;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            this.SmsConfig = new SmsConfig();

            SmsConfig.Password = "5AC3C61463FE17801E6A65490FFCB62D";
            SmsConfig.SmsAPIUrl = "http://sms.amyone.com/sms";
            SmsConfig.UserId = "djdnmq";
        }
        private string HttpGet(string action, string Datas, HttpAccessType ac)
        {
            string ReUrl = "";
            string line = "";
            string param = "?user=" + SmsConfig.UserId + "&pass=" + SmsConfig.Password + Datas;
            byte[] bs = Encoding.ASCII.GetBytes(param);

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(SmsConfig.SmsAPIUrl + param);
            req.Method = ac.ToString();
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; .NET CLR 1.1.4322)";
            req.Proxy = null;       //绕过代理,快速发送

            WebResponse wr = req.GetResponse();
            Stream strm = wr.GetResponseStream();
            StreamReader sr = new StreamReader(strm, System.Text.Encoding.UTF8);

            while ((line = sr.ReadLine()) != null)
            {
                ReUrl += line;
            }
            strm.Close();
            return ReUrl;
        }

        public List<Model.ZSJL.com.Net.SmsModel> GetReplySMSTable()
        {
            throw new NotImplementedException();
        }

        public List<Model.ZSJL.com.Net.SmsModel> GetReplySMSTable(DateTime time)
        {
            throw new NotImplementedException();
        }



        public bool SendSms(Model.ZSJL.com.Net.SmsModel smsModel)
        {

            if (string.IsNullOrEmpty(smsModel.PhoneNo) || smsModel.PhoneNo.Length < 11)
                throw new Exception("nonum电话号码为空！");
            string Datas = "";

            Datas += "&" + "phones" + "=" + smsModel.PhoneNo;
            Datas += "&" + "content" + "=" + HttpUtility.UrlEncode(smsModel.Content, Encoding.GetEncoding("UTF-8"));

            string result = HttpGet("SendSms", Datas, HttpAccessType.GET);

            //成功返回？
            if (result.IndexOf("+OK") > -1)
                return true;
            else
                throw new Exception(result);
        }

        public int CheckSumSms()
        {
            string ul = "http://sms.amyone.com/user/" + SmsConfig.UserId;
            SmsConfig.SmsAPIUrl = ul;
            string val = HttpGet("", "", HttpAccessType.GET);
            Mess m = val.DecodeJson<Mess>();
            return m.data.number;
        }
    }

    class Mess
    {

        public string error { get; set; }
        public datas data { get; set; }
    }

    class datas
    {

        public int number { get; set; }
    }
}
